package org.xukai.rest.common;

import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

/**
 * @author xukai
 * @desc
 * @date 2016-12-29-下午 6:11
 */
public enum RespCode {

    //服务 [000000]-[099999]
    SUCCESS(0, RespCodeType.COMMON, "成功!"),
    EXCEPTION(1, RespCodeType.COMMON, "处理发生异常"),
    ERROR(2, RespCodeType.COMMON, "服务暂不可用"),

    //限制 [100000]-[199999]
    LI_CALL_TIMES(100000, RespCodeType.LIMIT, "调用次数超过限制!"),
    LI_WRONG_PWD(100001, RespCodeType.LIMIT, "密码次数超过限制!"),
    //验证 [200000]-[299999]
    INVALID_ARGS(200000, RespCodeType.INVALID, "请求参数无效!"),
    INVALID_LOGIN_ID(200001, RespCodeType.INVALID, "账户ID错误!"),
    INVALID_PASSWORD(200002, RespCodeType.INVALID, "账户密码错误!"),
    INVALID_EXT_APPID(210001, RespCodeType.INVALID, "外部平台ID错误!"),
    INVALID_EXT_UID(210002, RespCodeType.INVALID, "外部用户ID错误!"),
    ;

    /**
     * 存放CODE -> Enum的Map
     */
    private static final ConcurrentMap<Integer, RespCode> CODE_MAP = new ConcurrentHashMap<Integer, RespCode>(
            RespCode.values().length);

    /**
     * 填充CODE --> Enum的Map
     */
    static {
        for (RespCode respCode : RespCode.values()) {
            CODE_MAP.put(respCode.code, respCode);
        }
    }

    /**
     * 响应码
     */
    private final int code;

    /**
     * 响应消息
     */
    private final String message;

    /**
     * Code类型
     */
    private final RespCodeType codeType;

    /**
     * 构造函数
     *
     * @param code    响应码
     * @param message 响应消息
     */
    RespCode(int code, RespCodeType codeType, String message) {
        this.code = code;
        this.codeType = codeType;
        this.message = message;
    }

    public static RespCode fromCode(Integer code) {
        return CODE_MAP.get(code);
    }

    public int code() {
        return code;
    }

    public RespCodeType codeType() {
        return this.codeType;
    }

    public String message() {
        return message;
    }
}
